var UIPanel_login = (function () {
    function UIPanel_login() {
        this._view = fairygui.UIPackage.createObject("Package", "login").asCom;
        this._view.setSize(fairygui.GRoot.inst.width, fairygui.GRoot.inst.height);
        fairygui.GRoot.inst.addChild(this._view);
        window._view = this._view;
        window.mainPanel = this;
        this.init();
    }

    var __proto = UIPanel_login.prototype;

    __proto.init = function () {
        this.account = this._view.getChild("account")
        this.password = this._view.getChild("password");
        this.btn_sure = this._view.getChild("btn_sure");
        this.btn_zc = this._view.getChild("btn_zc");
        this.btn_wj = this._view.getChild("btn_wj");

        this.account.text = localStorage.getItem('userAccount');
        this.password.text = localStorage.getItem('userPassrd');


        this.regis = this._view.getChild("regis");
        this.forget = this._view.getChild("forget");

        this.ctl_switch = this._view.getController("ctl_switch");

        this.btn_sure.sign = '登录';
        this.btn_sure.onClick(this, this.clickSure);
        this.initRegis();
        this.initForget();
    }
    __proto.initRegis = function () {
        this.phoneRegis = this.regis.getChild("phone");
        this.numRegis = this.regis.getChild("num");
        this.nameRegis = this.regis.getChild("name");
        this.psw1Regis = this.regis.getChild("psw1");
        this.psw2Regis = this.regis.getChild("psw2");

        var btn_back = this.regis.getChild("btn_back");
        var btn_sure = this.regis.getChild("btn_sure");
        var btn_getNum = this.regis.getChild("btn_getNum");
        var btn_wechat = this.regis.getChild("btn_wechat");
        var btn_protocol = this.regis.getChild("btn_protocol");

        btn_sure.sign = '注册';
        btn_getNum.phone = this.phoneRegis;

        btn_back.onClick(this, this.clickBack);
        btn_sure.onClick(this, this.clickSure);
        btn_getNum.onClick(this, this.clickGetNum);
        btn_wechat.onClick(this, this.clickWechat);
    }
    __proto.initForget = function () {
        this.phoneForget = this.forget.getChild("phone");
        this.numForget = this.forget.getChild("num");
        this.settingPassw = this.forget.getChild("settingPassw");

        var btn_back = this.forget.getChild("btn_back");
        var btn_sure = this.forget.getChild("btn_sure");
        var btn_getNum = this.forget.getChild("btn_getNum");

        btn_sure.sign = '忘记';

        btn_getNum.phone = this.phoneForget;

        btn_back.onClick(this, this.clickBack);
        btn_sure.onClick(this, this.clickSure);
        btn_getNum.onClick(this, this.clickGetNum);

        this.initSettingPassw();
    }
    __proto.initSettingPassw = function () {
        this.psw = this.settingPassw.getChild("psw1");
        this.pswSure = this.settingPassw.getChild("psw2");

        var btn_back = this.settingPassw.getChild("btn_back");
        var btn_sure = this.settingPassw.getChild("btn_sure");

        btn_sure.sign = '设置';

        btn_back.onClick(this, function () {
            this.settingPassw.visible = false;
        });
        btn_sure.onClick(this, this.clickSure);
    }
    __proto.clickGetNum = function (e) {
        console.log('获取验证码');
        var obj = e.target.$owner;
        var phone = obj.phone.text

        NetHttp.getCode(phone).then(function (res) {
            var data = res.data;
            if (data.code == 200) {
                Tip.instance().show("获取数字验证码成功，请注意查看验证码");
                Util.countdown_Code(obj);
            } else {
                Tip.instance().show(data.msg);
            }
        }).catch(function (err) {
            console.log(err);
            if (err.message.indexOf('Network Error') >= 0) {
                Tip.instance().show("网络连接失败, 请稍后再试");
                return;
            }
            Tip.instance().show("系统异常, 请稍后再试");
        });
    }
    __proto.clickWechat = function () {
        console.log('微信登录');
    }
    __proto.clickSure = function (e) {
        var obj = e.target.$owner;
        console.log(obj.sign);
        switch (obj.sign) {
            case '登录': {
                var account = this.account.text;
                var passrd = this.password.text;
                console.log('账号: ' + account);
                console.log('密码: ' + passrd);
                var self = this;
                NetHttp.login(account, passrd).then(function (res) {
                    var data = res.data;
                    if (data.code === 200) {
                        userInfo.setUserInfo(data);
                        userInfo.setToken(data.data.token);
                        self.setAccountPassrd(account, passrd);

                        console.log(userInfo.getUserData());

                        NetTcp.instance().loginGame();
                    } else {
                        console.log(data.msg)
                        Tip.instance().show(data.msg);
                    }
                }).catch(function (err) {
                    Tip.instance().show(err);
                });
                break;
            }
            case '注册': {
                var phone = this.phoneRegis.text;
                var code = this.numRegis.text;
                var name = this.nameRegis.text;
                var psw1 = this.psw1Regis.text;
                var psw2 = this.psw2Regis.text;
                console.log('手机号: ' + phone);
                console.log('验证码: ' + code);
                console.log('用户名: ' + name);
                console.log('密码: ' + psw1);
                console.log('确认密码: ' + psw2);
                if (phone === '') {
                    Tip.instance().show('请输入手机号');
                    return;
                }
                if (code === '') {
                    Tip.instance().show('请输入验证码');
                    return;
                }
                if (name === '') {
                    Tip.instance().show('请输入用户名');
                    return;
                }
                if (psw1 === '') {
                    Tip.instance().show('请输入密码');
                    return;
                }
                if (psw1 != psw2) {
                    Tip.instance().show('两次密码输入不一致！');
                    return;
                }
                var self = this;
                NetHttp.register(phone, psw1, name, code);
                break;
            }
            case '忘记': {
                console.log(this.phoneForget.text, this.numForget.text);
                if (this.phoneForget.text === '') {
                    Tip.instance().show('请输入手机号');
                    return;
                }
                if (this.numForget.text === '') {
                    Tip.instance().show('请输入验证码');
                    return;
                }
                this.settingPassw.visible = true;
                break;
            }
            case '设置': {
                console.log(this.psw.text, this.pswSure.text);
                if (this.psw.text != this.pswSure.text) {
                    Tip.instance().show('两次密码输入不一样');
                    return;
                }
                console.log(this.phoneForget.text, this.pswSure.text, this.numForget.text);
                var self = this;
                NetHttp.reset(this.phoneForget.text, this.pswSure.text, this.numForget.text).then(function (res) {
                    var data = res.data;
                    if (data.code == 200) {
                        Tip.instance().show("重置密码成功");
                        self.ctl_switch.selectedIndex = 0;
                        self.settingPassw.visible = false;
                    } else {
                        Tip.instance().show(data.msg);
                    }
                }).catch(function (err) {
                    Log.log(err);
                    if (err.message.indexOf('Network Error') >= 0) {
                        Tip.instance().show("网络连接失败, 请稍后再试");
                        return;
                    }
                    Tip.instance().show("系统异常, 请稍后再试");
                });
                break;
            }
        }
        //this.gamePanel = new UIPanel_Main();
    }
    __proto.clickBack = function () {
        this.ctl_switch.selectedIndex = 0;
    }

    //  localStorage  记住账号 密码
    __proto.setAccountPassrd = function (account, passrd) {
        localStorage.setItem('userAccount', account);
        localStorage.setItem('userPassrd', passrd);

    }


    return UIPanel_login;
})()
